package org.compiere.process;

import java.awt.Container;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import javax.swing.JFrame;

import org.compiere.apps.IProcessParameter;
import org.compiere.apps.ProcessCtl;
import org.compiere.apps.ProcessParameterPanel;
import org.compiere.apps.Waiting;
import org.compiere.model.Mhrmvacationschedule;
import org.compiere.util.ASyncProcess;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;

/**
 * Class allows you to print a report directly to the form
 * Vacation Schedule Report
 * @author V.Sokolov
 *
 */
public class VacationScheduleReport extends SvrProcess implements ASyncProcess {

	/** */
	private int AD_Process_ID;
	/** */
	private Mhrmvacationschedule vacation = null;
	/** */
	ASyncProcess m_parent;
	
	@Override
	protected void prepare() {
		
		String orderType = "generateVacationSchedule";
		vacation = new Mhrmvacationschedule(getCtx(), getRecord_ID(), get_TrxName());
		
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		// The name must start with a report hrm_order
		String sql_ = "select distinct t.ad_process_id from ad_process t "+ 
					  "where (lower(t.value) like lower('hrm_%') or lower(t.name) like lower('hrm_%')) "+ 
				      "and (lower(t.value) like lower('%"+orderType+"%') or lower(t.name) like lower('%"+orderType+"%'))";
		try
		{
			pstmt = DB.prepareStatement(sql_, null);
			rs = pstmt.executeQuery();
			if (rs.next())
				AD_Process_ID = rs.getInt(1);
		}
		catch (SQLException e)
		{
			log.log(Level.SEVERE, "product", e);
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}	

	}

	@Override
	protected String doIt() throws Exception {
		
		if(AD_Process_ID == 0)
			return  "The process can not be found";
		//  Prepare Process
		ProcessInfo pi = new ProcessInfo ("The printing process of the order", AD_Process_ID);
		pi.setAD_User_ID (Env.getAD_User_ID(Env.getCtx()));
		pi.setAD_Client_ID(Env.getAD_Client_ID(Env.getCtx()));

		List<ProcessInfoParameter> po = new ArrayList<ProcessInfoParameter>();
		po.add(new ProcessInfoParameter("HR_Block_ID", new BigDecimal(vacation.getHR_Block_ID()),null,"",""));
		po.add(new ProcessInfoParameter("HR_Department_ID", new BigDecimal(vacation.getHR_Department_ID()),null,"",""));
		po.add(new ProcessInfoParameter("hrm_years_ID", new BigDecimal(vacation.gethrm_years_ID()),null,"",""));
		po.add(new ProcessInfoParameter("report", "N",null,"",""));
		//
		ProcessInfoParameter[] pp = new ProcessInfoParameter[po.size()];
		po.toArray(pp);
		pi.setParameter(pp);
		//	Execute Process
		ProcessParameterPanel pu = new ProcessParameterPanel(0, pi);
		ProcessCtl.process(this, 0, (IProcessParameter) pu, pi, null);
		
		return "View the report Vacation Schedule";
	}
	
	private Waiting	m_waiting;

	@Override
	public void lockUI(ProcessInfo pi) {
		JFrame frame = Env.getFrame((Container)m_parent);
		m_waiting = new Waiting (frame, Msg.getMsg(Env.getCtx(), "Processing"), false, pi.getEstSeconds());
		
	}

	@Override
	public void unlockUI(ProcessInfo pi) {
		if (m_waiting != null)
			m_waiting.dispose();
		m_waiting = null;
	}

	@Override
	public boolean isUILocked() {
		return false;
	}

	@Override
	public void executeASync(ProcessInfo pi) {
		
	}

}
